package cn.cuilan.jol;

import org.openjdk.jol.info.ClassLayout;
import org.openjdk.jol.vm.VM;

import java.util.concurrent.TimeUnit;

/**
 * https://hg.openjdk.java.net/code-tools/jol/file/551b7317a04e/jol-samples/src/main/java/org/openjdk/jol/samples
 *
 * @author zhang.yan
 * @since 2021/3/8
 */
public class FatLockTest {

    public static void main(String[] args) throws InterruptedException {
        System.out.println(VM.current().details());

        final A a = new A();
        ClassLayout layout = ClassLayout.parseInstance(a);

        System.out.println("创建对象");
        System.out.println(layout.toPrintable());

        Thread t = new Thread(() -> {
            synchronized (a) {
                try {
                    TimeUnit.SECONDS.sleep(10);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });

        t.start();

        TimeUnit.SECONDS.sleep(1);

        System.out.println("线程争抢锁之前");
        System.out.println(layout.toPrintable());

        synchronized (a) {
            System.out.println("同步代码块");
            System.out.println(layout.toPrintable());
        }

        System.out.println("同步代码块执行完成");
        System.out.println(layout.toPrintable());

        System.gc();

        System.out.println("gc之后");
        System.out.println(layout.toPrintable());

        synchronized (a) {
            System.out.println("同步代码块");
            System.out.println(layout.toPrintable());
        }
    }

}
